home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / datatypes / playdt13.lha / PlayDT / PlayDT-src / PlayFile.c < prev    next >
C/C++ Source or Header  |  1997-06-05  |  5KB  |  161 lines

  1. /****************************************************************
  2.  *
  3.  * $VER: PlayFile.c 1.3 (4.6.97) Tak Tang (tst92@ecs.soton.ac.uk)
  4.  *
  5.  * Copyright © 1997 Tak Tang
  6.  *
  7.  * You may use any part of this source code in your own programs
  8.  * provided that it is not called PlayDT.
  9.  *
  10.  *****************************************************************/
  11.  
  12.  
  13. /**** Header files ****/
  14.  
  15. #include "PlayDT.h"
  16.  
  17. #include <exec/execbase.h>
  18.  
  19. #include <datatypes/datatypes.h>
  20. #include <datatypes/datatypesclass.h>
  21. #include <datatypes/soundclass.h>
  22.  
  23. #include <clib/dos_protos.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/datatypes_protos.h>
  26.  
  27. #include <pragmas/dos_pragmas.h>
  28. #include <pragmas/exec_pragmas.h>
  29. #include <pragmas/datatypes_pragmas.h>
  30.  
  31.  
  32. /****** PlayFile ***************************************************
  33. *
  34. *   NAME
  35. *       PlayFile -- Play a sample using the sound datatype.
  36. *
  37. *   SYNOPSIS
  38. *       error = PlayFile( GlobalData, FileName )
  39. *
  40. *       ULONG = PlayFile( struct tGlobalData *, STRPTR );
  41. *
  42. *   FUNCTION
  43. *       This routine plays a single file using the sound datatype.
  44. *       If the procedure receives a CTRL-C or a CTRL-D, it will halt
  45. *       the current sound.  In addition, a CTRL-C will set the
  46. *       gd->UserStop flag, to indicate that no further samples should
  47. *       be played.
  48. *
  49. *   INPUTS
  50. *       GlobalData - A pointer to structure containing library bases
  51. *                        and other global data.
  52. *
  53. *       FileName   - The filename of the sound sample to play, relative
  54. *                        to the current directory.  This is not checked
  55. *                        to see if it is a valid filename.
  56. *
  57. *   RESULT
  58. *       error           - ) if NewDTObject was OK, 1 otherwise
  59. *       (gd->UserStop)  - This flag is set if the sound playback was
  60. *                             interrupted by a CTRL-C
  61. *
  62. *   EXAMPLE
  63. *
  64. *   NOTES
  65. *       Errors in NewDTObject are now reported back to calling function.
  66. *
  67. *       There are two methods to wait for the sound to finish.  The 3.0
  68. *       sound datatype requires that we calculate the duration of the
  69. *       sample, and do a delay.  This function checks for CTRL-C and
  70. *       CTRL-D each second.  A better way to do this would be to set up
  71. *       a timer request and doing a wait(), but I need to research this.
  72. *       The KS3.1 and Jonathan Gapen's sound datatypes can signal the
  73. *       process when the sound has finished playing.
  74. *
  75. *   BUGS
  76. *
  77. *   SEE ALSO
  78. *       sound.datatype, alib/DoMethod()
  79. *
  80. *****************************************************************************
  81. *
  82. */
  83.  
  84.  
  85. ULONG PlayFile(struct GlobalData *gd, STRPTR file)
  86. {
  87.   Object *o;
  88.   ULONG signal;
  89.   ULONG rc=1;
  90.  
  91.   o = NewDTObject (          (APTR)file,
  92.       DTA_SourceType,        DTST_FILE,
  93.       DTA_GroupID,           GID_SOUND,
  94.       SDTA_Volume,           64,
  95.       SDTA_Cycles,           1,
  96.       TAG_DONE);
  97.  
  98.   if ( NULL != o )
  99.   {
  100.  
  101.     /* Clear signals */
  102.     SetSignal(0L, SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
  103.  
  104.     /* Attempt to use KS3.1 signalling */
  105.     if ( 2 == SetDTAttrs(o, NULL, NULL,
  106.          SDTA_SignalTask,       gd->MeTask,
  107.          SDTA_SignalBit,        (ULONG) SIGBREAKF_CTRL_D,
  108.          TAG_END) )
  109.     {
  110.       /* Play the sound using KS3.1 signal */
  111.       DoDTMethod(o, NULL, NULL, DTM_TRIGGER, NULL, STM_PLAY, NULL);
  112.       signal=Wait (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
  113.     } /* if can use KS3.1 signal */
  114.     else
  115.     {
  116.       ULONG length = 0, period = 394, cycles = 1, ticks;
  117.  
  118.       /* Obtain information from the object. */
  119.       GetDTAttrs(o, SDTA_SampleLength, &length,
  120.          SDTA_Period, &period,    SDTA_Cycles, &cycles,
  121.          TAG_END);
  122.  
  123.       /* Calculate duration of the sound. */
  124.       ticks = 1 + ( length * cycles * period ) /
  125.               (gd->SysBase->ex_EClockFrequency * 5);
  126.  
  127.       /* Start the sound. */
  128.       DoDTMethod(o, NULL, NULL, DTM_TRIGGER, NULL, STM_PLAY, NULL);
  129.  
  130.       /* Play, checking each second if user tried to abort */
  131.       /* The proper way to do this, of course, is to set up a
  132.          timer to signal in ticks seconds, then wait.  If the
  133.          user aborts early, then cancel the timer.
  134.       */
  135.       for ( signal=0; ticks && (0==signal) ; ticks --)
  136.       {
  137.         Delay(50);
  138.         signal=SetSignal(0L, 0L) & (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D) ;
  139.       }
  140.     } /* if cannot use KS3.1 signal */
  141.  
  142.     /* if user sent a CTRL-C, let calling function know */
  143.     if (signal & SIGBREAKF_CTRL_C)
  144.     {
  145.       gd->UserStop=TRUE;
  146.     } /* if user pressed CTRL-C */
  147.  
  148.     /* Get rid of the object */
  149.     DisposeDTObject (o);
  150.  
  151.     /* Playing succedded, right? */
  152.     rc=0;
  153.   } /* if NewDTObject() ok */
  154.  
  155.   return rc;
  156. } /* PlayFile() */
  157.  
  158.  
  159. /**** End of file ****/
  160.  
  161.